package dataStructure.xmg1.a07.tree;

import java.util.Comparator;

/**
 * @author: vince_zww
 * @date: 2021/1/2 10:43
 * @desceription:
 */
public class AVLTree<E> extends BST<E> {

    public AVLTree() {
        this(null);
    }

    public AVLTree(Comparator comparator) {
        super(comparator);
    }

    @Override
    protected void afterAdd(Node<E> node) {
        while ((node = node.parent) != null) {
            if (isBalanced(node)) {
                //如果是平衡，更新高度
                updateHeight(node);
            } else {
                //如果不平衡，恢复平衡
                rebalance(node);
                //整颗树恢复平衡
                break;
            }
        }
    }

    @Override
    protected void afterRemove(Node<E> node) {
        while ((node = node.parent) != null) {
            if (isBalanced(node)) {
                //如果是平衡，更新高度
                updateHeight(node);
            } else {
                //如果不平衡，恢复平衡
                rebalance(node);
            }
        }
    }

    @Override
    protected Node<E> createNode(E element,Node<E> parent){


        return new AVLNode<>(element,parent);
    }

    /**
     * 恢复平衡
     * @param grand 高度最低的那个不平衡节点
     *  这个是原本方法，改为2
     */
    private void rebalance2(Node<E> grand) {
        //找到比较高的子节点
        Node<E> parent = ((AVLNode<E>) grand).tallerChild();
        Node<E> node = ((AVLNode<E>) parent).tallerChild();
        if (parent.isLeftChild()) { //L
            if (node.isLeftChild()) {//LL
                rotateRight(grand);
            } else { //LR
                rotateLeft(parent);
                rotateRight(grand);
            }
        } else { //R
            if (node.isLeftChild()) { //RL
                rotateRight(parent);
                rotateLeft(grand);
            } else { //RR
                rotateLeft(grand);
            }
        }
    }

    /**
     * 恢复平衡
     * @param grand 高度最低的那个不平衡节点
     */
    private void rebalance(Node<E> grand) {
        //找到比较高的子节点
        Node<E> parent = ((AVLNode<E>) grand).tallerChild();
        Node<E> node = ((AVLNode<E>) parent).tallerChild();
        if (parent.isLeftChild()) { //L
            if (node.isLeftChild()) {//LL
                rotate(grand,node.left,node,node.right,parent, parent.right,grand,grand.right);
            } else { //LR
                rotate(grand,parent.left,parent,node.left,node,node.right,grand,grand.right);
            }
        } else { //R
            if (node.isLeftChild()) { //RL
                rotate(grand,grand.left,grand,node.left,node,node.right,parent,parent.right);
            } else { //RR

                rotate(grand,grand.left,grand,parent.left,parent,node.left,node,node.right);
            }
        }
    }

    /**
     * 统一旋转
     */
    private void rotate(
            Node<E> r,//子树根节点
            Node<E> a, Node<E> b, Node<E> c,
            Node<E> d,
            Node<E> e, Node<E> f, Node<E> g) {
        //让d成为这课子树的根节点
        d.parent = r.parent;
        if (r.isLeftChild()) {
            r.parent.left = d;
        } else if (r.isRightChild()) {
            r.parent.right = d;
        } else {
            root = d;
        }
        //a-b-d
        b.left = a;
        if (a != null) {
            a.parent = b;
        }
        b.right = c;
        if (c != null) {
            c.parent = b;
        }
        updateHeight(b);

        //e-f-g
        f.left = e;
        if (e != null) {
            e.parent = f;
        }
        f.right = g;
        if (g != null) {
            g.parent = f;
        }
        updateHeight(f);

        //b-d-f
        d.left = b;
        d.right = f;
        b.parent = d;
        f.parent = d;
        updateHeight(d);


    }

    //左旋转
    private void rotateLeft(Node<E> grand) {
        Node<E> parent = grand.right;
        Node<E> child = parent.left;
        grand.right = child;
        parent.left = grand;

        afterRotate(grand, parent, child);
    }

    //右旋转
    private void rotateRight(Node<E> grand) {
        Node<E> parent = grand.left;
        Node<E> child = parent.right;
        grand.left = child;
        parent.right = grand;

        afterRotate(grand, parent, child);
    }

    //统一旋转之后的处理
    private void afterRotate(Node<E> grand,Node<E> parent,Node<E> child){
        //更新parent的parent属性
        //让parent成为子树的根节点
        parent.parent = grand.parent;
        if (grand.isLeftChild()) {
            grand.parent.left = parent;
        } else if (grand.isRightChild()) {
            grand.parent.right = parent;
        } else {// grand是root节点
            root = parent;
        }

        //更新child的parent属性
        if (child != null) {
            child.parent = grand;
        }

        //更新grand的parent属性
        grand.parent = parent;

        //最后更新高度
        updateHeight(grand);
        updateHeight(parent);
    }


    //是否平衡
    private boolean isBalanced(Node<E> node) {
        return Math.abs(((AVLNode<E>) node).balanceFactor()) <= 1;
    }

    private void updateHeight(Node<E> node) {
        ((AVLNode<E>) node).updateHeight();

    }

    //AVL节点
    private static class AVLNode<E> extends Node<E> {
        //叶子节点高度默认是1
        int height = 1;
        public AVLNode(E element, Node<E> parent) {
            super(element, parent);
        }

        public int balanceFactor() {
            int leftHeight = left == null ? 0 : ((AVLNode<E>)left).height;
            int rightHeight = right == null ? 0 : ((AVLNode<E>)right).height;
            return leftHeight - rightHeight;
        }

        public void updateHeight() {
            int leftHeight = left == null ? 0 : ((AVLNode<E>)left).height;
            int rightHeight = right == null ? 0 : ((AVLNode<E>)right).height;
            height = 1 + Math.max(leftHeight, rightHeight);
        }

        public Node<E> tallerChild() {
            int leftHeight = left == null ? 0 : ((AVLNode<E>)left).height;
            int rightHeight = right == null ? 0 : ((AVLNode<E>)right).height;
            if (leftHeight > rightHeight) return left;
            if (leftHeight < rightHeight) return right;
            return isLeftChild() ? left : right;
        }

        @Override
        public String toString() {
//        Node<E> myNode = (Node<E>) node;
            String parentString = "null";
            if (parent != null) {
                parentString = parent.element.toString();
            }
            return element + "_p(" + parentString + ")_h" + height + ")";
        }
    }



}
